import {h} from "vue";
import {formatDateTime, timePlusZero} from "../util/utils";
import config from "../util/config";
import {FormItemRule} from "naive-ui";
import store from "../store";
import {InternalRowData, TableColumn} from "naive-ui/es/data-table/src/interface";

const jobCycleMap: Map<number, string> = store().enumMapStore["JobCycle"];

export class AddMetaDataParam {
    applicationName?: string;
    jobClassName?: string;
    jobName?: string;
    jobRetryTimes?: number;
    jobRetryInterval?: number;
    jobCycle?: number;
}

export class UpdateMetaDataParam extends AddMetaDataParam {
    id?: bigint;
}

export class PageQueryMetaDataParam {
    applicationName?: string;
    jobClassName?: string;
    jobName?: string;
}

export class JobMetaDataVO extends UpdateMetaDataParam {
    operatorName?: string;
    updateTime?: number;
    hasSchedule?: boolean;
}

export const basicColumns: TableColumn<InternalRowData>[] = [
    {
        title: "应用名称",
        key: "applicationName",
        ellipsis: {
            tooltip: true
        }
    },
    {
        title: "任务类名",
        key: "jobClassName",
        ellipsis: {
            tooltip: true
        }
    },
    {
        title: "任务名称",
        key: "jobName",
        ellipsis: {
            tooltip: true
        }
    },
    {
        title: "失败重试次数",
        key: "jobRetryTimes"
    },
    {
        title: "失败重试间隔(分钟)",
        key: "jobRetryInterval"
    },
    {
        title: "执行周期",
        key: "jobCycle",
        render: (rowData: JobMetaDataVO) => {
            return "每" + jobCycleMap.get(rowData.jobCycle as number);
        }
    },
    {
        title: "操作人姓名",
        key: "operatorName"
    },
    {
        title: "更新时间",
        key: "updateTime",
        width: config.dateTimeColumnWidth,
        render: (rowData: JobMetaDataVO) => {
            return h("span", formatDateTime(rowData.updateTime));
        }
    }
];

export const rules = {
    applicationName: {
        required: true,
        message: "请输入应用名称",
        trigger: ["blur"]
    },
    jobClassName: {
        required: true,
        message: "请输入任务类名",
        trigger: ["blur"]
    },
    jobName: {
        required: true,
        message: "请输入任务名称",
        trigger: ["blur"]
    },
    jobRetryTimes: {
        type: "number",
        required: true,
        message: "请输入失败重试次数",
        validator: (_rule: FormItemRule, value: number): boolean => {
            return value > 0;
        },
        trigger: ["blur"]
    },
    jobCycle: {
        type: "number",
        required: true,
        message: "请选择执行周期",
        validator: (_rule: FormItemRule, value: number): boolean => {
            return value > 0;
        },
        trigger: ["blur"]
    },
    jobRetryInterval: {
        type: "number",
        required: true,
        message: "请输入失败重试间隔",
        validator: (_rule: FormItemRule, value: number): boolean => {
            return value > 0;
        },
        trigger: ["blur"]
    },
};

const monthList = [];
for (let i = 1; i <= 12; i++) {
    monthList.push({label: timePlusZero(i), value: i});
}
export const monthOptions = monthList;

export const weekOptions = [
    {
        label: "星期一",
        value: 1
    },
    {
        label: "星期二",
        value: 2
    },
    {
        label: "星期三",
        value: 3
    },
    {
        label: "星期四",
        value: 4
    },
    {
        label: "星期五",
        value: 5
    },
    {
        label: "星期六",
        value: 6
    },
    {
        label: "星期日",
        value: 7
    },
];

const dayList = [];
for (let i = 1; i <= 31; i++) {
    dayList.push({label: timePlusZero(i), value: i});
}
export const dayOptions = dayList;

const hourList = [];
for (let i = 0; i <= 23; i++) {
    hourList.push({label: timePlusZero(i), value: i});
}
export const hourOptions = hourList;

const minuteList = [];
for (let i = 0; i <= 59; i++) {
    minuteList.push({label: timePlusZero(i), value: i});
}
export const minuteOptions = minuteList;

const secondList = [];
for (let i = 0; i <= 59; i++) {
    secondList.push({label: timePlusZero(i), value: i});
}
export const secondOptions = secondList;

